﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Linq;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace WindowsFormsApplication1.SensorTable
{
    public partial class FormAlarm : Form
    {
        MONITOR_DBDataContext alarm_context = new MONITOR_DBDataContext();
        public FormAlarm()
        {
            InitializeComponent();
        }

        private void dataGridViewZoneColumnRefresh()
        {
            //为地区列赋值
            for (int i = 0; i < tb_alarmDataGridView.Rows.Count; i++)
            {
                if (tb_alarmDataGridView.Rows[i].Cells[dataGridViewTextBoxColumn2.Name].Value != null)
                {
                    int mon_id = Convert.ToInt32(tb_alarmDataGridView.Rows[i].Cells[dataGridViewTextBoxColumn3.Name].Value.ToString());

                    // 获取当前行监控箱号
                    IEnumerable<tb_monitor_device> qZone =
                    from mon in alarm_context.tb_monitor_device
                    where mon.ID == mon_id
                    select mon;

                    foreach (var item in qZone)
                    {
                        tb_alarmDataGridView.Rows[i].Cells[zone.Name].Value = item.Zone;
                    }
                }
            }
        }

        private void tb_alarmBindingNavigatorSaveItem_Click(object sender, EventArgs e)
        {
            DataTable daorig = new DataTable();
            DataTable dadest = new DataTable();

            int[] check = new int[13] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 };
            try
            {
                SqlConnection sqlCon = new SqlConnection(UserLog.connectionstring);
                SqlDataAdapter sda = new SqlDataAdapter();
                SqlDataAdapter sda2 = new SqlDataAdapter();


                string strSql = "select * from tb_alarm";
                SqlCommand cmd = new SqlCommand(strSql, sqlCon);
                sda.SelectCommand = cmd;
                sda.Fill(daorig);
                tb_alarmDataGridView.EndEdit();
                alarm_context.SubmitChanges();
      

                MessageBox.Show("保存成功！", "", MessageBoxButtons.OK, MessageBoxIcon.Information);

                sda.Fill(dadest);


                UserLog.adduseroperlog(User.UserID, User.Username, "告警确认", "成功", "批量", "批量", "批量确认");
                
            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message, "错误！", MessageBoxButtons.OK, MessageBoxIcon.Error);
                Monitor_DB.instance.Refresh_DB();
            }


        }

        private void FormAlarm_Load(object sender, EventArgs e)
        {
            try
            {
                alarm_context = new MONITOR_DBDataContext();
                IEnumerable<tb_alarm> alarm =
                    from al in alarm_context.tb_alarm
                    where al.Checked == false
                    select al;

                this.tb_alarmBindingSource.DataSource = alarm;


                // 刷新地区列
                dataGridViewZoneColumnRefresh();
            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message, "错误！", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void tb_alarmDataGridView_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            // 刷新地区列
            dataGridViewZoneColumnRefresh();
        }

        private void splitContainer1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Control == true && e.KeyCode == Keys.S)
            {
                tb_alarmBindingNavigatorSaveItem_Click(sender, e);
            }
        }

        private void tb_alarmDataGridView_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Control == true && e.KeyCode == Keys.S)
            {
                tb_alarmBindingNavigatorSaveItem_Click(sender, e);
            }
        }

        private void toolStripButton_refresh_Click(object sender, EventArgs e)
        {
            //alarm_context.Refresh(RefreshMode.OverwriteCurrentValues, alarm_context.tb_alarm);
            alarm_context.Dispose();
            alarm_context = new MONITOR_DBDataContext();
            IEnumerable<tb_alarm> alarm =
                from al in alarm_context.tb_alarm
                where al.Checked == false
                select al;

            this.tb_alarmBindingSource.DataSource = alarm;

            this.tb_alarmDataGridView.Refresh();
        }

        private void FormAlarm_FormClosing(object sender, FormClosingEventArgs e)
        {
            alarm_context.Dispose();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            IEnumerable<tb_alarm> alarm =
                from al in alarm_context.tb_alarm
                where al.Checked == false && al.Alarm_Type.Contains(comboBox1.Text)
                && al.Sensor.Contains(comboBox2.Text)
                select al;

            this.tb_alarmBindingSource.DataSource = alarm;

            this.tb_alarmDataGridView.Refresh();

            // 刷新地区列
            dataGridViewZoneColumnRefresh();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (this.tb_alarmDataGridView.Rows.Count == 0)
            {
                MessageBox.Show("请先搜索要确认的告警！", "确认告警", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
            else
            {
                for (int i = 0; i < this.tb_alarmDataGridView.RowCount; i++)
                {
                    this.tb_alarmDataGridView.Rows[i].Cells["Checked"].Value = true;
                }

                DialogResult r = MessageBox.Show("是否保存？", "确认告警", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

                if (r == DialogResult.Yes)
                {
                    tb_alarmBindingNavigatorSaveItem_Click(tb_alarmBindingNavigatorSaveItem, new EventArgs());
                }
            }
        }

        
    }
}
